package com.ynkbny.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ynkbny.base.PageResult;
import com.ynkbny.domain.DevicePoint;
import com.ynkbny.domain.pojo.DevicePointPaginReqVO;
import com.ynkbny.domain.pojo.DevicePointPaginResponseVO;
import com.ynkbny.domain.pojo.DevicePointVO;
import com.ynkbny.mapper.DevicePointMapper;
import com.ynkbny.service.DevicePointService;
import com.ynkbny.util.SearchUtil;
import com.ynkbny.util.UserRoleUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * 设备点位表-表服务实现类
 *
 * @author : juzipi123
 * @date : 2023-8-2
 */
@Slf4j
@Service
public class DevicePointServiceImpl extends ServiceImpl<DevicePointMapper, DevicePoint> implements DevicePointService {

    @Resource
    DevicePointMapper mapper;
    @Resource
    private UserRoleUtil<DevicePointPaginResponseVO> userRoleUtil;
    @Resource
    private UserRoleUtil<DevicePoint> userRoleUtil1;

    /**
     * 条件查询
     *
     * @author : juzipi123
     * @param vo 筛选条件
     * @return 结果
     */
    @Override
    public List<DevicePoint> queryByDevicePointPaginVO(DevicePointVO vo) {
        log.info("入参：{}",vo);

        //1. 构建动态查询条件
        LambdaQueryWrapper<DevicePoint> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtils.isNotEmpty(vo.getProjectId())) {
            queryWrapper.eq(DevicePoint::getProjectId, vo.getProjectId());
        }
        if (ObjectUtils.isNotEmpty(vo.getEnterpriseCode())) {
            queryWrapper.eq(DevicePoint::getEnterpriseCode, vo.getEnterpriseCode());
        }
        if (ObjectUtils.isNotEmpty(vo.getFlag())) {
            queryWrapper.eq(DevicePoint::getFlag, vo.getFlag());
        }
        if (ObjectUtils.isNotEmpty(vo.getEscalation())) {
            queryWrapper.eq(DevicePoint::getEscalation, vo.getEscalation());
        }
        queryWrapper.orderByDesc(DevicePoint::getProjectId);


        //2. 返回结果
        List<DevicePoint> list = this.list(queryWrapper);
        log.info("方法返回：{}",list);
        return list;
    }

    /**
     * 分页查询
     *
     * @author : juzipi123
     * @param vo 筛选条件
     * @return 查询结果
     */
    @Override
    public PageResult<DevicePointPaginResponseVO> paginQuery(DevicePointPaginReqVO vo) {
        Page<DevicePointPaginReqVO> page = new Page<>(vo.getCurrentPage(), vo.getPageSize());
        //前端查询传回来的参数
        QueryWrapper<DevicePointPaginResponseVO> queryWrapper=new QueryWrapper<DevicePointPaginResponseVO>()
                .eq(StringUtils.isNotBlank(vo.getEscalation()),"dp.escalation",vo.getEscalation())
                .eq(StringUtils.isNotBlank(vo.getProjectId()),"dp.project_id",vo.getProjectId());
        QueryWrapper<DevicePointPaginResponseVO> queryWrapper1 = userRoleUtil.getQueryWrapper(queryWrapper);
        PageResult<DevicePointPaginResponseVO> devicePointPaginResponseVOPageResult = new PageResult<>(this.mapper.queryAllByLimit(page, queryWrapper1));
        return devicePointPaginResponseVOPageResult;
    }

    /**
     * 根据 ‘企业上传数据 开始时间’ 查询 设备点位表
     *
     * @author : juzipi123
     * @param startTime 企业上传数据 开始时间
     * @return 查询结果
     */
    @Override
    public List<DevicePoint> selectByStartTime(String startTime){
        log.info("入参：{}",startTime);
        List<DevicePoint> devicePoints = mapper.selectByStartTime(startTime);
        log.info("方法返回：{}",devicePoints);
        return devicePoints;
    }

    @Override
    public List<DevicePoint> selectByEscalation() {
        return mapper.selectByEscalation();
    }

    /**
     *
     * @param conditionJson 分页信息
     * @param pageNum 页数
     * @param pageSize 页条数
     * @return
     */
    @Override
    public PageResult<DevicePoint> pageList(String conditionJson, int pageNum, int pageSize) {
        Page<DevicePoint> page = new Page<>(pageNum, pageSize);
        QueryWrapper<DevicePoint> queryWrapper = SearchUtil.parseWhereSql(null, null, conditionJson);
        Page<DevicePoint> pageList = userRoleUtil1.getPageList(page, queryWrapper, this.baseMapper);
        return new PageResult<>(pageList);
    }
}